package com.jml.controller;


import com.jml.service.IMessageService;
import com.jml.util.Member;
import com.jml.util.controller.AbstractBaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MessageController extends AbstractBaseController {

	@Resource
	private IMessageService messageService;

	@RequestMapping(value = "/echo/{message}",method = RequestMethod.GET)
	@ResponseBody
	public String echo(@PathVariable("message") String msg){
		System.out.println("[***访问地址***]:"+super.getMessage("welcome.url"));
		return super.getMessage("welcome.msg",msg);
		//这个msg就对应  welcome.msg=欢迎{0}光临！中的{0}  表示的是参数
	}

	/*@RequestMapping(value = "/echo", method = RequestMethod.GET)
	public String echo(String mid) {
		System.out.println("【*** 访问地址 ***】" + super.getMessage("member.add.action"));
		return super.getMessage("welcome.msg", mid);
	}*/


	@RequestMapping(value = "/", method = RequestMethod.GET)
	@ResponseBody
	public String Index() {
		return messageService.info();
	}


	private Logger log=LoggerFactory.getLogger(MessageController.class);
	@RequestMapping("/abc")
	@ResponseBody
	public String home(){
		log.info("info----------");
		log.error("error----------");
		return "21321";
	}

	@RequestMapping("/show")
	public String show(String mid,Model model){
		model.addAttribute("url","www.jml.com");
		model.addAttribute("mid",mid);
		return "message/message_show";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}

	@RequestMapping("/showStyle")
	public String showStyle(Model model){
		model.addAttribute("url","<span style='color:red'>www.jml.com</span>");
		return "message/message_show_style";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}

	@RequestMapping("/message/inner")
	public String inner(HttpServletRequest request,Model model){
		request.setAttribute("requestcolor","<span style='color:red'>www.jml.com</span>");
		request.setAttribute("request","www.jml.com");
		request.getSession().setAttribute("sessionMsg","session");
		request.getServletContext().setAttribute("applicationMsg","application");
		model.addAttribute("msg","jml");
		return "message/message_show_inner";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}

	@RequestMapping("/message/member_show")
	public String memberShow(HttpServletRequest request,Model model){
		Member member =new Member();
		member.setMid(101L);
		member.setName("阿三");
		member.setAge(9);
		member.setSalary(9999.99);
		member.setBirthday(new Date());
		model.addAttribute("member",member);
		return "message/member_show";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}

	@RequestMapping("/message/list")
	public String list(HttpServletRequest request,Model model){
		ArrayList<Member> allMembers = new ArrayList<>();
		for (int i = 0; i <10 ; i++) {
			Member member =new Member();
			member.setMid(101L+i);
			member.setName("阿三"+i);
			member.setAge(9);
			member.setSalary(9999.99);
			member.setBirthday(new Date());
			allMembers.add(member);
		}
		model.addAttribute("allMembers",allMembers);
		return "message/member_list";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}


	@RequestMapping("/message/map")
	public String map(HttpServletRequest request,Model model){
		Map<String,Member> allMembers = new HashMap<>();
		for (int i = 0; i <10 ; i++) {
			Member member =new Member();
			member.setMid(101L+i);
			member.setName("阿三"+i);
			member.setAge(9);
			member.setSalary(9999.99);
			member.setBirthday(new Date());
			allMembers.put("jml"+i,member);
		}
		model.addAttribute("allMembers",allMembers);
		return "message/member_map";
		//此处只返回一个路径，没有后缀，后缀默认是*.html,也可以修改
	}

	@RequestMapping("/message/set")
	public String set(HttpServletRequest request,Model model){
		Set<String> allNames=new HashSet<String>();
		Set<Integer> allIds= new HashSet<Integer>();
		for (int i = 0; i <5 ; i++) {
			allNames.add("jml"+i);
			allIds.add(i);
		}
		model.addAttribute("names",allNames);
		model.addAttribute("ids",allIds);
		model.addAttribute("myDate",new Date());
		return "message/member_set";
	}
}
